<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>

		<script type="text/javascript">
			function Human(name, sex, birthday) {
				this.name = name;
				this.sex = sex;
				this.birthday = birthday;

				this.getAge = function() {
					let date = new Date();
					let year = date.getFullYear();
					let month = date.getMonth() + 1;
					let day = date.getDate()

					let birthDate = new Date(this.birthday);
					let birthYear = birthDate.getFullYear();
					let birthMonth = birthDate.getMonth();
					let birthDay = birthDate.getDate();

					var monthDiff = month - birthMonth; //月差
					if (monthDiff <= 0) {
						var dayDiff = day - birthDay; //日差
						if (dayDiff > 0) {
							return year - birthYear + 1;
						} else {
							return year - birthYear;
						}
					} else {
						return year - birthYear;
					}
				}
			}
			
			let human = new Human("张三", "男", "2001-10-10");	//21,日期改为09-10则显示20
			let age = human.getAge();
			console.log(age);
			
		</script>
	</body>
</html>
